<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属导航" prop="navigateId">
        <el-select v-model="queryParams.navigateId" placeholder="请选择所属导航" clearable size="small">
          <el-option
            v-for="item in goodsCategoryOptions"
            :key="item.navigateId"
            :label="item.navigateName"
            :value="item.navigateId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="频道名称" prop="channelName">
        <el-input
          v-model="queryParams.channelName"
          placeholder="请输入频道名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="频道类型" prop="channelType">
        <el-select v-model="queryParams.channelType" placeholder="请选择频道类型" clearable size="small">
          <el-option
            v-for="item in channelTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:channel:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:channel:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:channel:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="channelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属导航" align="center" prop="navigateName"/>
      <el-table-column label="频道名称" align="center" prop="channelName"/>
      <el-table-column label="频道图片" align="center" prop="channelImg">
        <template scope="scope">
          <el-popover placement="right" title="" trigger="hover">
            <img :src="scope.row.channelImg | imgPathFilter" style="width: auto;">
            <img slot="reference" :src="scope.row.channelImg | imgPathFilter"
                 :alt="scope.row.channelImg | imgPathFilter" style="max-height: 50px;max-width: 130px">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="频道类型" align="center" prop="channelType">
        <template scope="scope">
          <span v-if="scope.row.channelType==1">轮播图频道</span>
          <span v-if="scope.row.channelType==2">功能频道</span>
          <span v-if="scope.row.channelType==3">广告频道</span>
        </template>
      </el-table-column>
      <el-table-column label="跳转地址" align="center" prop="channelUrl"/>
      <el-table-column label="跳转地址类型" align="center" prop="channelUrlType" :formatter="channelUrlTypeFormat"/>
      <el-table-column label="排序" align="center" prop="channelSort"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:channel:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:channel:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改电商频道对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属导航" prop="navigateId">
          <el-select v-model="form.navigateId" placeholder="请选择所属导航" clearable size="small">
            <el-option
              v-for="item in goodsCategoryOptions"
              :key="item.navigateId"
              :label="item.navigateName"
              :value="item.navigateId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="频道名称" prop="channelName">
          <el-input v-model="form.channelName" placeholder="请输入频道名称"/>
        </el-form-item>
        <el-form-item label="频道图片" prop="channelImg">
          <el-upload
            class="avatar-uploader"
            :action="upload.url"
            :show-file-list="false"
            :headers="upload.headers"
            name="file"
            accept=".jpg, .png"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.channelImg" :src="form.channelImg | imgPathFilter" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="频道类型" prop="channelType">
          <el-select v-model="form.channelType" placeholder="请选择频道类型" clearable size="small">
            <el-option
              v-for="item in channelTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="跳转地址" prop="channelUrl">
          <el-input v-model="form.channelUrl" placeholder="请输入跳转地址"/>
        </el-form-item>
        <el-form-item label="跳转地址类型" prop="jumpType">
          <el-select v-model="form.channelUrlType" placeholder="请选择跳转地址类型">
            <el-option
              v-for="item in channelUrlTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="channelSort">
          <el-input-number v-model="form.channelSort" placeholder="请输入排序"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { getToken } from '@/utils/auth'
import { queryGoodsListNavigate } from '@/api/app/navigate'
import { listChannel1, getChannel, delChannel, addChannel, updateChannel, exportChannel } from '@/api/app/goodsChannel'

export default {
  name: 'Channel',
  components: {},
  data() {
    return {
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/common/upload',
        // 上传的文件列表
        fileList: []
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电商频道表格数据
      channelList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        navigateId: null,
        channelName: null,
        channelImg: null,
        channelType: null,
        channelUrl: null,
        channelSort: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        channelName: [
          { required: true, message: '频道名称不能为空', trigger: 'blur' }
        ],
        channelUrl: [
          { required: true, message: '跳转地址不能为空', trigger: 'blur' }
        ],
        channelSort: [
          { required: true, message: '排序不能为空', trigger: 'blur' }
        ]
      },
      channelTypeOptions: [{ value: 1, label: '轮播图频道' }, { value: 2, label: '功能频道' }, { value: 3, label: '广告频道' }],
      goodsCategoryOptions: [],
      channelUrlTypeOptions: [{ value: 'taobao', label: '淘宝链接' }, { value: 'other', label: '其他链接' }]
    }
  },
  created() {
    this.getList()
    this.getGoodsListNavigate()
  },
  methods: {
    /** 查询电商频道列表 */
    getList() {
      this.loading = true
      listChannel1(this.queryParams).then(response => {
        this.channelList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getGoodsListNavigate() {
      let params = { navigateCodes: 'taobao,tianmao' }
      queryGoodsListNavigate(params).then(response => {
        this.goodsCategoryOptions = response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        goodsChannelId: null,
        navigateId: null,
        channelName: null,
        channelImg: null,
        channelType: null,
        channelUrl: null,
        channelUrlType: null,
        channelSort: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsChannelId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    channelUrlTypeFormat(row, column) {
      let channelUrlType = this.channelUrlTypeOptions.find(item => item.value == row.channelUrlType)
      if (channelUrlType) {
        return channelUrlType.label
      } else {
        return '无'
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加电商频道'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const goodsChannelId = row.goodsChannelId || this.ids
      getChannel(goodsChannelId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改电商频道'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.goodsChannelId != null) {
            updateChannel(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addChannel(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsChannelIds = row.goodsChannelId || this.ids
      this.$confirm('是否确认删除电商频道编号为"' + goodsChannelIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delChannel(goodsChannelIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    handleAvatarSuccess(res, file) {
      this.form.channelImg = res.data.objectID
    },
    beforeAvatarUpload(file) {
      const isLt5M = file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('上传导航图标大小不能超过 5MB!')
      }
      return isLt5M
    }
  }

}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
